from ModCalc import Calc
from modCalculatableNumber import CalculatableNumber
from modCalculatablePolynomial import CalculatablePolynomial
from modMatrix import Matrix
from modLogicSignals import LogicSignals
'''
writen by Phillip Toussaint
Catches input from the calculator GUI, and sends formated signals to the calculator controler
Handles string conversion for number input and stack display
'''
class Translator():

    def __init__(self, sender):
        self.calc = Calc()
        self.parent = sender
        self.last=1
    
    def sendChar(self, char):

        data = self.parent.getText()
        type = self.parent.getType()
        self.last = data
        self.parent.clearScreen()
        try:
            if not data=="":
                if type == "N: ":
                    self.calc.addItem(CalculatableNumber(eval(data)))

                if type == "P: ":
                    self.calc.addItem(CalculatablePolynomial(eval(data)))

                if type == "M: ":
                    self.calc.addItem(Matrix(eval(data)))

                if type == "L: ":
                    self.calc.addItem(LogicSignals(eval(data)))
                    
        except:
            char = " "
            self.last = None
            
        if char == "+":
            self.last = self.calc.add()
            
        if char == "-":
            self.last = self.calc.subtract()
            
        if char == "*":
            self.last = self.calc.multiply()

        if char == "/":
            self.last = self.calc.divide()

        if char == "I":
            self.last = self.calc.invert()

        if char == "N":
            self.last = self.calc.negate()
        
        if char == "M":
            self.last = self.calc.createPoly()
            
        if char == "S":
            self.last = self.last

        if char == "C":
            self.last = self.calc.pop()

        if char == "D":
            self.last=self.last

        if char == "Q":
            self.last = self.calc.square()

        if not self.last==None:
            for x in range(0,5):
                self.parent.setStack(x, self.calc.peek(x).__str__())
        else:
            self.parent.setStack(0, "Error")
            for x in range(1,5):
                self.parent.setStack(x, self.calc.peek(x-1).__str__())
        
        
    def push(self, object):
        self.calc.addItem(object)
